'''
Created on 2012-7-7

@author: wb_suoyao.shen
'''
from django.db import models
from django.db import connection

class CommentManager(models.Manager):
    def get_comment_count(self,diaryId):
        sql = '''SELECT count(*) FROM comment_comment WHERE diary_id = %s'''
        cursor = connection.cursor()
        cursor.execute(sql,diaryId)
        row = cursor.fetchone()
        if row:
            count = row[0]
        else:
            count = 0
        return count

class Comment(models.Model):
    diary_id = models.IntegerField(null=True)
    author_name = models.CharField(max_length=256,null=True,blank=True)
    author_email = models.EmailField(blank=True,null=True)
    author_url = models.URLField(blank=True,null=True)
    content = models.CharField(max_length=2560,null=True,blank=True)
    comment_time = models.DateTimeField(null=True,blank=True)
    client_ip = models.CharField(max_length=256,blank=True,null=True)
    objects = CommentManager()
    
    def __unicode__(self):
        return self.author_name
    
    class Meta:
        ordering = ['-comment_time']
        
    class Admin:
        list_display = ('diary_id','author_name','author_email','author_url','content','comment_time','client_ip')
        fields = (
            (None,{'fields':('diary_id','author_name','author_email','author_url','content','comment_time','client_ip')}),
        )